


/**
 * 
 * @author Aram Taieb 2011-11-28
 * @modified Aram Taieb 2011-12-01
 * @param {Object} config 
 * @param {Object} config.levelImages
 */
function Puzzle(config) {
    var me = this;
    
    me.el = document.getElementById("puzzle_container");
    
    me.levelImages = config.levelImages;
    me.setImg(config.size);
    
    // Sizes
    me.puzzleSize = me.el.offsetWidth-40;
    
//    console.log("cubeSize: " + me.cubeSize.toString());
    
    me.callbackWhenSolved  = config.callbackWhenSolved;
    me.callbackScope = config.scope;
    
    me.onHorseFaceShownCallback = config.onHorseFaceShown;
    
    
    // The magical horse cube page index 
    me.horseIdx = 0;
    
    me.createCubeGrid(config.size);
    me.shuffle(); 
}

Puzzle.prototype = {
/**
 * 
 * @author Hans Calmfors 2011-12-01
 */
    createCubeGrid : function(size) {
        var me = this,
        cube,
//        var positionvar = {x:0, y:0};
        cubeSize = parseInt(me.puzzleSize/size-8, 10),
        i, j;
        
        me.size = size;
        me.cubes = [];
        me.setImg(size);
        // row
        for (i=0;i<size;i++)  {
            // One array for each row
            me.cubes.push([]);

            // column
            for (j=0; j<size; j++) {
//                positionvar.x = parseInt(j*cubeSize, 10);
//                positionvar.y = parseInt(i*cubeSize, 10);
//
//                console.log("position.x:" + positionvar.x);
//                console.log("position.y:" + positionvar.y);

                cube = new Cube({
                    size: cubeSize,
                    rotateCallback : {
                        fn: me.onCubeClickedCallback,
                        scope: me
                    },
                    onHorseFaceShown: me.onHorseFaceShownCallback,
                    position: {
                        x : parseInt(j*cubeSize, 10),
                        y : parseInt(i*cubeSize, 10)
                    },
                    src: me.imgSrc
                });

                // TODO: change cube instaniation
                me.cubes[i].push(cube);

                // add to DOM
                me.el.appendChild(cube.getEl());
            }
        }
    },
    
    clearCubeGrid : function() {
        var me = this,
        cubes = me.cubes,
        i, j;
        for (i=0;i<cubes.length;i++)  {
            for (j=0; j<cubes[i].length; j++) {
                me.el.removeChild(cubes[i][j].el);
            }
        }
    },
/**
 * 
 * @author Aram Taieb 2011-11-28
 * @modified Aram Taieb 2011-11-29
 */
    shuffle : function() {
        
//        console.log("shuffling cubes!");
        var me = this,
        i, j,
        s = me.size;

        for (i=0; i<s; i++) {
            for (j=0; j<s; j++) {
                //console.log("shuffling cube (" + i + ", " + j + ")");
                me.cubes[i][j].shuffle();
            }
        }
    },

/**
 * 
 * @author Aram Taieb 2011-11-28
 * @modified Aram Taieb 2011-11-29
 */
    isSolved : function() {
        // If solved
        // Display message
        var me = this,
        i,j,
        s = me.size;

        for (i=0; i<s; i++) {
            for (j=0; j<s; j++) {
                if (me.cubes[i][j].getFaceId() !== me.horseIdx) {
//                    console.log(i + " " + j);
                    return false;
                }
            }
        }
        
//        console.log("SOLVED");
        
        return true;
    
    },

/**
 * 
 * @author Aram Taieb 2011-11-28
 * @modified Aram Taieb 2011-11-29
 */
    onCubeClickedCallback : function() {
        var me = this;

        if (me.isSolved()) {
            
                       
            me.callbackWhenSolved.apply(me.callbackScope, arguments);
        }
    },
    
    setImg : function(puzzleSize) {
        var me = this,
        imgs = me.levelImages,
        img,
        level = puzzleSize-3; // 1,2,3 ...
        
        if (imgs[level] === undefined) {
            img = "img/" + imgs[ Math.round(Math.random() * (imgs.length-1)) ];
        }
        else {
            img = "img/" + imgs[level];
        }
        me.imgSrc = img;
        
        
    },
    
    highlightFrame : function() {
        
        var me = this;
        me.el.style.borderColor = "#8b65bd";
        
        setTimeout(function() {
            me.el.style.borderColor = "#381764";
        }, 200);
        
    }
};

